package com.xuxin.pda.video;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.shuyu.gsyvideoplayer.GSYVideoManager;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;
import com.xuxin.pda.R;
import com.xuxin.pda.base.BaseActivity;
import com.xuxin.pda.utils.RecycleUtils;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by xu_xin
 * Created on 2020.06.30 0030
 */
public class VideoListActivity extends BaseActivity {
    @BindView(R.id.title_left)
    ImageView title_left;
    @BindView(R.id.title_right)
    ImageView title_right;
    @BindView(R.id.title_name)
    TextView title_name;
    @BindView(R.id.smart_refresh)
    SmartRefreshLayout smart_refresh;
    @BindView(R.id.smart_recycle)
    RecyclerView smart_recycle;

    private VideoAdapter myAdapter;

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_smart);
    }

    @Override
    public void initView() {
        title_name.setText("视频列表");
        //刷新加载
        smart_refresh.setOnRefreshListener(new OnRefreshListener() {

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                onComplete();
            }
        });
        RecycleUtils.init(smart_recycle);
        myAdapter = new VideoAdapter();
        smart_recycle.setAdapter(myAdapter);
    }

    @Override
    public void initData() {
        List<String> video_list = new ArrayList<>();
        video_list.add("http://9890.vod.myqcloud.com/9890_4e292f9a3dd011e6b4078980237cc3d3.f20.mp4");
        video_list.add("http://9890.vod.myqcloud.com/9890_4e292f9a3dd011e6b4078980237cc3d3.f20.mp4");
        video_list.add("http://9890.vod.myqcloud.com/9890_4e292f9a3dd011e6b4078980237cc3d3.f20.mp4");
        video_list.add("http://9890.vod.myqcloud.com/9890_4e292f9a3dd011e6b4078980237cc3d3.f20.mp4");
        video_list.add("http://9890.vod.myqcloud.com/9890_4e292f9a3dd011e6b4078980237cc3d3.f20.mp4");
        myAdapter.setList(video_list);
        smart_refresh.autoRefresh();
    }

    @Override
    public void onComplete() {
        smart_refresh.finishRefresh();
        smart_refresh.finishLoadMore();
    }

    @Override
    @OnClick({R.id.title_left})
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.title_left) {
            finish();
        }
    }

    /**
     * video
     */
    @Override
    public void onBackPressed() {
        if (GSYVideoManager.backFromWindowFull(this)) {
            return;
        }
        super.onBackPressed();
    }

    @Override
    protected void onPause() {
        super.onPause();
        GSYVideoManager.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        GSYVideoManager.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        GSYVideoManager.releaseAllVideos();
    }

    /**
     * video
     */
    public static class VideoAdapter extends BaseQuickAdapter<String, BaseViewHolder> {

        public VideoAdapter() {
            super(R.layout.item_video);
        }

        @Override
        protected void convert(@NotNull BaseViewHolder holder, String item) {
            StandardGSYVideoPlayer item_video_player = holder.getView(R.id.item_video_player);
            item_video_player.setUpLazy(item, true, null, null, "这是title");
            item_video_player.getTitleTextView().setVisibility(View.GONE);
            item_video_player.getBackButton().setVisibility(View.GONE);
            item_video_player.getFullscreenButton().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    item_video_player.startWindowFullscreen(getContext(), false, true);
                }
            });
            item_video_player.setPlayTag(String.valueOf(holder.getAdapterPosition()));
            item_video_player.setPlayPosition(holder.getAdapterPosition());
            item_video_player.setAutoFullWithSize(true);
            item_video_player.setReleaseWhenLossAudio(false);
            item_video_player.setShowFullAnimation(false);
            item_video_player.setIsTouchWiget(false);
        }
    }
}